package com.usion.service;

import cn.hutool.jwt.JWTUtil;
import cn.jiyun.entity.ResultClass;
import cn.jiyun.entity.day13.User;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author sjt
 * @Date 2023/8/22 11:11
 * @PackageName: com.usion.service.UserService
 */
@Service
public class UserService {
    @Autowired
    UserFeginService userFeginService;
    @Autowired
    RedisTemplate redisTemplate;


    public ResultClass userLogin(User user) {
        try{
            String token = userFeginService.userLogin(user);
            if(StringUtils.isBlank(token))return ResultClass.error();

            return ResultClass.ok(token).Message("登录成功");
        } catch (Exception e) {
            return ResultClass.error().Message("登录失败");
        }
    }

    public ResultClass findLoginUser(HttpServletRequest request) {
        try{
            String token = request.getHeader("token");
            User user = JSON.parseObject(String.valueOf(redisTemplate.opsForHash().get("用户登录信息", token)), User.class);
//            System.out.println(user);
            if(user!=null)return ResultClass.ok(user);
            return ResultClass.error();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultClass.error();
        }
    }
}
